/**
 * Created by fupeng on 17/3/9.
 */
var movieCollectionDao =  require('../dao/MovieCollectionDao');
var memberDao = require('../dao/MemberDao');
var cdn = 'http://cdn.bestmovie.com.cn/MOVIE/';
var poster = '/POSTER/';

module.exports = {
    updateMovieCollection:function (req,callback) {
        try{
            var mid = req.body.mid;
            var open_id = req.signedCookies.openid||req.signedCookies.open_id;
            if(open_id){
                memberDao.selectByopenid(open_id,function(data){
                    if(data.rows[0]){
                        var member_id = data.rows[0].member_id;
                        var param = [member_id,mid];
                        movieCollectionDao.selectMovieCollectionByMemberidAndMid(param,function(data){
                            if(data.isSuccess){
                                if(data.rows[0]){
                                    movieCollectionDao.deleteMovieCollectionByMemberidAndMid(param,function (data) {
                                        if(data.isSuccess){
                                            callback({
                                                status: 'success',
                                                message:'取消收藏成功'
                                            });
                                        }else{
                                            callback({
                                                status: 'error',
                                                message:'取消收藏失败'
                                            });
                                        }
                                    });
                                }else{
                                    movieCollectionDao.addMovieCollectionByMemberidAndMid(param,function (data) {
                                        if(data.isSuccess){
                                            callback({
                                                status: 'success',
                                                message:'收藏成功'
                                            });
                                        }else{
                                            callback({
                                                status: 'error',
                                                message:'收藏失败'
                                            });
                                        }
                                    });
                                }

                            }
                        });
                    }else{
                        callback({
                            status: 'error',
                            message:'请先注册会员'
                        });
                    }

                })
            }
        }catch (error){
            console.log('moviecollectioncontroller:error'+error.stack);
            callback({
                status: 'error',
                message:'error'
            });
        }


    },
    getMovieInfoLimitModel:function(req, callback){
        if (/^\d+$/.test(req.body.lastIndex)) {
            if(req.body.open_id) {
                memberDao.selectByopenid(req.body.open_id, function (data) {
                    if (data.rows[0]) {
                        var member_id = data.rows[0].member_id;
                        movieCollectionDao.getMovie({
                            member_id: member_id,
                            movie_name: req.body.movie_name,
                            lastIndex: parseInt(req.body.lastIndex)
                        }, function (data) {
                            var movieInfo = [];
                            if (data.isSuccess) {
                                data.rows.forEach(function (val) {
                                    movieInfo.push({
                                        mid: val.mid,                       //影片编号
                                        movie_name: val.movie_name,         //影片名字
                                        director: val.director,              //导演
                                        brief: val.brief,                    //详情介绍
                                        cast: val.cast,                    //演员
                                        score: val.score,                    //评分
                                        country: val.country,                //地区
                                        category: val.category,               //类型-标签
                                        publishdate: val.publishdate,        //影片-年
                                        poster: cdn + val.mid + poster + val.poster
                                    });

                                });
                                callback({
                                    message: 'success',
                                    status: 'success',
                                    data: movieInfo
                                });
                            } else {
                                callback({
                                    message: 'error',
                                    status: 'error',
                                    data: ''
                                });
                            }
                        })
                    }
                })
            }else{
                callback({
                    message: 'error',
                    status: 'error',
                    data: ''
                });
            }
        }else{
            callback({
                message: 'error',
                status: 'error',
                data: ''
            });
        }
    }
};
